﻿package game.lang {
	
	import fl.lang.Locale;
	import flash.text.TextField;
	import flash.events.Event;
	import game.Config;
	
	public class Lang extends Locale {
		
		public static const RU:String	= "ru";
		public static const EN:String	= "en";
		
		private var objects:Vector.<LangObject> = new Vector.<LangObject>;
		
		public function Lang(lang:String):void {
			Locale.autoReplace = true;
			
			//устанавлвиаем ссылки на языковые xml из config'a
			var config:Config = new Config;
			for (var l:String in config.lang)
				Locale.addXMLPath(l, config.host + config.dirs.lang + config.lang[l]);
			
			Locale.setLoadCallback(localeCallback);
			
			local = lang;
		}
		
		public function set local(value:String):void {
			
			Locale.setDefaultLang(value);
			Locale.loadLanguageXML(value);
			
		}
		
		public function get local():String { return Locale.getDefaultLang(); }
		
		private function localeCallback(success:Boolean):void {
			if (success) {
				for each (var object:LangObject in objects) {
					object.tf.text = parse(object);
					object.tf.dispatchEvent( new Event ( Event.CHANGE ) );
				}
			}
		}
		
		private function parse(object:LangObject):String {
			var str:String;
			
			try {
				str = Locale.loadString(object.ids);
			} catch (e:Error) {
				
			}
			
			if (str)
				str = regexp(str, object.params ? object.params : new Object);
			else
				str = object.ids;
			
			return str;
		}
		
		public static function regexp(str:String, data:Object):String {
			try {
				for (var i:String in data) { while (str.search(new RegExp('%'+i+'%', 'i'))!=-1) str = str.replace(new RegExp('%'+i+'%', 'i'), data[i]);  } 
				//склонение слов: число слово[массив 3х склонений] //example: 1 пользовател[ь,я,ей]
				var reg:RegExp = new RegExp('(\\d+)(\\s+[а-яА-Я]+)\\[([а-яА-Я,]+)\\]', 'i');
				while (str.search(reg)!=-1) str = str.replace(reg, reg.exec(str)[1]+reg.exec(str)[2]+getNumberVariation(reg.exec(str)[1], reg.exec(str)[3].split(',')));
			} catch (e:Error) {
				//
			}
			return str;
		}
		
		public static function getNumberVariation(number:Number, variation:Array):String {
			var suffix:String = '';
			if (number==0) suffix = variation[2];
			else if (number==1) suffix = variation[0];
			else if (number>=2 && number<=4) suffix = variation[1];
			else if (number>=5 && number<=20) suffix = variation[2];
			else if (number>=21 && number%10==1) suffix = variation[0];
			else if (number>=21 && number%10>=2 && number%10<=4) suffix = variation[1];
			else suffix = variation[2];
			
			return suffix;
		}
		
		public function set(tf:TextField, ids:String, params:Object = null, multi:Boolean = true):void {
			if (!multi) {
				//если строка не мультиязычная
				tf.text = regexp(ids, (params) ? params : new Object);
				return;
			}
			
			var object:LangObject = new LangObject (tf, ids, params);
			
			if (parse(object)) {
				tf.text = parse(object);
				tf.dispatchEvent( new Event ( Event.CHANGE ) );
			}
			
			objects.push( object );
			
			if (parse(object)!=object.ids)
				Locale.addDelayedInstance(tf, ids);
			
			
		}
		
	}
	
}